﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion 

namespace Ramparts
{
    class Cannonball : GameComponent
    {
        public Vector2 startingPosition = Vector2.Zero;
        public Vector2 endingPosition = Vector2.Zero;
        public Vector2 velocity = Vector2.Zero;
        public Vector2 currentPosition = Vector2.Zero;
        public float scale;
        public int heightOffset;
        public float totalDistance;
        public float currentDistance;

        public Cannonball(String textureName, Vector2 position, SceneManager inSceneManager, bool useCamera)
        {
            // copy the attributes of the base game component
            base.sceneManager = inSceneManager;
            base.position = position;
            base.useCamera = useCamera;
            base.texture = base.sceneManager.rampart.content.Load<Texture2D>(textureName);

            // permanently set the cannon ball's position to the cannon's location
            startingPosition = position;
            currentPosition = position;
            scale = 1;
            heightOffset = 0;
        }

        public void ResetCannonBall()
        {
            this.alive = false;
            this.position = startingPosition;
            this.endingPosition = Vector2.Zero;
            this.velocity = Vector2.Zero;
            this.currentPosition = startingPosition;
        }

        public void Fire(Vector2 targetPos)
        {
            Vector2 totalDistanceVector = startingPosition - targetPos;
            totalDistance = totalDistanceVector.Length();
            velocity = new Vector2((targetPos.X - startingPosition.X)/totalDistance, (targetPos.Y - startingPosition.Y)/totalDistance);
            alive = true;
            endingPosition = targetPos;



        }

        public void Update()
        {
            if (alive)
            {
                currentPosition += velocity *4;
                Vector2 currentDistanceVector = startingPosition - currentPosition;
                currentDistance = currentDistanceVector.Length();
                scale = (float)Math.Sin((double)(currentDistance / totalDistance) * Math.PI) + 1;
                heightOffset = (int)(Math.Sin((double)(currentDistance / totalDistance) * Math.PI) * (totalDistance/5));

                if (sceneManager.world.groundLayer.onTile(currentPosition) == sceneManager.world.groundLayer.onTile(endingPosition))
                {
                    ResetCannonBall();
                }
            }
        }

        public override void Draw()
        {
            if (alive)
            {
                sceneManager.rampart.spriteBatch.Draw(texture, new Vector2 (currentPosition.X, currentPosition.Y - heightOffset), 
                                                      null, Color.White, 0, new Vector2(5, 5), 
                                                      scale, SpriteEffects.None, 1);
            }
        }
    

    


    }
}
